#pragma once

#include "public/build.h"
#include "core/color.h"

#include <algorithm>

namespace renderit
{
	namespace messiah
	{
		class pixel_walker
		{
		public:
			pixel_walker(core::color4b* ppixel, int width, int height)
				: ppixel_(ppixel)
				, width_(width)
				, height_(height)
			{

			}
			
			pixel_walker(const pixel_walker& o)
			{
				operator = (o);
			}

		public:
			core::color4b &cur_pixel()
			{
				return ppixel_[curr_row_ * width_ + curr_line_];
			}

		public:
			pixel_walker& operator = (const pixel_walker& o)
			{
				memcpy(this, &o, sizeof(*this));
				return *this;
			}

		public:
			bool walk_to(int row, int line)
			{
				if (row >= 0 && row < height_ && line >= 0 && line < width_) {
					curr_row_ = row;
					curr_line_ = line;
					on_walk();
					return true;
				}
				return false;
			}

			bool walk_left()
			{
				if (curr_line_ > 0) {
					curr_line_--;
					on_walk();
					return true;
				}
				return false;
			}

			bool walk_right()
			{
				if (curr_line_ < width_ - 1) {
					curr_line_++;
					on_walk();
					return true;
				}
				return false;
			}

			bool walk_up()
			{
				if (curr_row_ > 0) {
					curr_row_--;
					on_walk();
					return true;
				}
				return false;
			}

			bool walk_down()
			{
				if (curr_row_ < height_ - 1) {
					curr_row_++;
					on_walk();
					return true;
				}
				return false;
			}

			int curr_row() const
			{
				return curr_row_;
			}

			int curr_line() const
			{
				return curr_line_;
			}

		protected:
			virtual void on_walk() {}

		protected:
			core::color4b* ppixel_;
			int width_;
			int height_;

		private:
			int curr_row_;
			int curr_line_;
		};
	}
}